<template>
  <!--  公话- 统计报表-   充值汇总表 -->
  <div class="recharge_summary_table">
    <el-form
      :inline="true"
      :model="elform"
      class="demo-form-inline"
      size="mini"
    >
      <el-form-item label="充值:" >
        <el-date-picker
          v-model="monthnum"
          value-format="yyyy-MM-31 23:59:59 "
          type="month"
          placeholder="选择月"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="gosearch"
          size="mini"
          icon="el-icon-search"
          >查询</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportexeclto" size="mini"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="tableinformation"
      style="width: 100%"
      border="border"
      :header-cell-style="headFirst"
      highlight-current-row
      :cell-style="(res) => headFirst_col(res, [])"
    >
      <el-table-column label="统计月份">
        <template slot-scope="scope">
          <div>{{ scope.row.adjustTime | filtertime }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="moneyCount" label="笔数"> </el-table-column>
      <el-table-column prop="adjustAmount" label="金额(元)">
        <template slot-scope="scope">
          <div>{{ scope.row.adjustAmount | amountjiao }}</div>
        </template>
      </el-table-column>
    </el-table>
        <div class="page">
      <el-pagination
        @size-change="handleSizeChangepage"
        @current-change="handleCurrentChangepage"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="recordCount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      elform: {},
      value1: "",
      value2: "",
      tableinformation: [],
      recordCount: 0,
      pageSize: 20,
      pageNum: 1,
      currentPage: 1,
      agentlistout: [],
      dataListLoading: false,
      monthnum: "",
      searchInfo: {},
      filename: "充值汇总表",
    };
  },
  mounted() {
    this.monthnum = this.getNowTime('yy-mm-dd 59');
  },
  filters: {
    filtertime: function (adjustTime) {
      if (adjustTime) {
        let testStr = adjustTime;
        let re = new RegExp("(.*)-", "ig");
        let r = re.exec(testStr);
        return r[1];
      }
    },
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid;
      },
    },
  },
  methods: {
    //当前月份
    gosearch() {
      this.pageNum = 1;
      this.searchInfo = {};
      if (this.monthnum !== null && this.monthnum !== "") {
        this.searchInfo.startDate = this.monthnum;
      }
      this.searchInfo.unitGuid = this.changeschoolid;
      this.detailsuminit();
    },
    detailsuminit() {
      if (this.monthnum == "" || this.monthnum == null) {
        this.$message({
          message: "请选择时间",
          type: "warning",
        });
        return;
      }
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/user/recharge/detail/info-detail-sum"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            searchInfo: this.searchInfo,
          },
          messageType: this.messageType,
        }),
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.tableinformation = data.messageContent.data;
          this.recordCount = data.messageContent.recordCount;
          this.currentPage = data.messageContent.pageNum;
          this.dataListLoading = false;
            if (this.tableinformation.length == 0) {
            this.$message({
              message: '查询成功当前暂无数据',
              type: 'success'
            })
          }
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    exportexeclto() {
      if (this.tableinformation.length == 0) {
        this.$message({
          message: "查询数据后导出",
          type: "warning",
        });
        return false;
      }
      this.outexecel();
    },
    outexecel() {
      this.$http({
        url: this.$http.adornUrl("/user/recharge/detail/info-detail-sum"),
        method: "post",
        data: this.$http.adornData({
          messageContent: {
            pageSize: this.recordCount,
            searchInfo: this.searchInfo,
          },
          messageType: 0,
        }),
      }).then(({ data }) => {
        if (data.messageCode === 200) {
          let jsonArr = data.messageContent.data;
          //  let re = /([0-9]+\.[0-9]{2})[0-9]*/;
          let re = new RegExp("(.*)-", "ig");
          for (var i = 0; i < jsonArr.length; i++) {
            jsonArr[i].adjustAmount = this.amountjia(jsonArr[i].adjustAmount);
            jsonArr[i].adjustTime = re.exec(jsonArr[i].adjustTime)[1];
          }
          this.agentlistout = jsonArr;
          setTimeout(() => {
            this.export2Excel();
          }, 100);
        } else {
          this.$message.error("导出遇未知错误");
        }
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) => filterVal.map((j) => v[j]));
    },
    export2Excel() {
      const nowtime = this.getimeto();
      require.ensure([], () => {
        const multiHeader = [];
        const multiHeader2 = [];
        const merges = [];
        const { export_json_to_excel } = require("../../../excel/Export2Excel");
        const tHeader = ["统计月份", "笔数", "金额(元)"];
        const filterVal = ["adjustTime", "moneyCount", "adjustAmount"];
        const list = this.agentlistout;
        const data = this.formatJson(filterVal, list);
        export_json_to_excel({
          multiHeader, //这里是第一行的表头
          multiHeader2, //这里是第二行的表头
          header: tHeader, //这里应该是算第三行的表头
          data,
          merges,
          filename: this.filename + nowtime,
        });
      });
    },
        handleSizeChangepage(val) {
      this.pageSize = val;
      this.detailsuminit();
    },
    handleCurrentChangepage(val) {
      this.pageNum = val;
      this.detailsuminit();
    },
  },
};
</script>

<style lang="scss" scoped>
.recharge_summary_table {
  padding: 10px;
  .demo-form-inline,
  .demo-form-inline1 {
    /deep/.el-form-item__label {
      padding-right: 5px;
    }
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-date-editor.el-input,
    .el-date-editor.el-input__inner {
      width: 150px;
    }
  }
    .page {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>